<template>
  <div class="app-container company-container" style="width: 700px;margin:0 auto">
    <div style="text-align: right; margin-bottom: 10px;">
      <el-button type="primary" @click="handleJoinOrg">加入企业</el-button>
    </div>
    <template v-if="orgInfos.length>0">
      <el-card v-for="item in orgInfos" :key="item.id" class="box-card">
        <div slot="header" class="clearfix">
          <span class="company-title">
            {{ item.name }}
            <template v-if="item.chargePerson">
              <el-tag v-if="item.chargePerson.split(':')[0]===userInfo.id" effect="plain"> 负责人 </el-tag>
            </template>
            <el-tag v-if="item.id===orgInfo.id" effect="plain"> 当前企业 </el-tag>
          </span>
          <el-button v-if="item.id===3" style="float: right; padding: 3px 0" type="text">
            <span class="color-warning">待审核,后期加入</span>
          </el-button>
        </div>
        <div class="content">
          <div class="left">
            <template v-if="item.chargePerson">
              <!--<span v-if="item.chargePerson.split(':')[0]===userInfo.id" class="color-danger" @click="handleDissolution(item)">解散企业</span>-->
              <span v-if="item.chargePerson.split(':')[0]!==userInfo.id" class="color-danger" @click="leaveOrg(item)">退出企业</span>
            </template>
            <span v-else class="color-danger" @click="leaveOrg(item)">退出企业</span>
          </div>
          <div class="center"><span @click="handleLookOrg(item)">查看企业资质</span></div>
          <div class="right">
            <span v-if="item.id!==orgInfo.id" class="color-primary" @click="enterOrg(item)">进入企业</span>
          </div>
        </div>
      </el-card>
    </template>
    <el-empty v-else description="还未加入任何组织" />
    <model-dialog
      :title="orgInfoRecord.organizationName+'企业资质'"
      :visible.sync="dialogVisible"
      width="900px"
      @close="dialogVisible=!dialogVisible">
      <el-form
        ref="dataForm"
        :model="orgInfoRecord"
        label-position="right"
        label-width="140px"
        style="margin:20px;overflow: auto;">
        <el-form-item label="标签:">
          <el-tag>{{ orgInfoRecord._organizationCategory }}</el-tag>
          <el-tag style="margin-left:8px" type="success">{{ orgInfoRecord._organizationLevel }}</el-tag>
          <el-tag v-for="tag in orgInfoRecord._organizationTags" :key="tag" style="margin-left:8px" type="danger">{{ tag }}</el-tag>
        </el-form-item>
        <el-form-item label="负责人:"> {{ orgInfoRecord.chargeInfo | userSelectorFormat }} </el-form-item>
        <el-form-item label="编码:"> {{ orgInfoRecord.organizationCode }} </el-form-item>
        <el-form-item label="名称:">{{ orgInfoRecord.organizationName }}</el-form-item>
        <el-form-item label="统一社会信用代码:">{{ orgInfoRecord.socialCreditCode }}</el-form-item>
        <el-form-item v-if="orgInfoRecord.businessLicense" label="营业执照:">
          <img :src="common.getFileView(orgInfoRecord.businessLicense)" class="common-cursor" style="width:210px" @click="common.handleFilePreview(orgInfoRecord.businessLicense)">
        </el-form-item>
        <el-form-item label="邀请码:">
          <copy-text :content="orgInfoRecord.invitationCode" position="left" />
          <span v-if="orgInfoRecord.invitationCode" class="color-primary common-cursor" style="margin-left:8px">
            <copy-text :content="inviteUrl+orgInfoRecord.invitationCode" position="left" />
          </span>
        </el-form-item>

        <el-form-item label="地址:"> {{ orgInfoRecord._regionLevel1+orgInfoRecord._regionLevel2+orgInfoRecord._regionLevel3+orgInfoRecord._regionLevel4 }}{{ orgInfoRecord.address }} </el-form-item>
        <el-form-item label="经纬度:"> {{ orgInfoRecord.latlng }} </el-form-item>
        <el-form-item label="电话:">{{ orgInfoRecord.phone }}</el-form-item>
        <el-form-item label="传真:">{{ orgInfoRecord.fax }}</el-form-item>
        <el-form-item label="联系人:">{{ orgInfoRecord.linkPerson }}</el-form-item>
        <el-form-item label="联系电话:">{{ orgInfoRecord.linkMobile }}</el-form-item>
        <el-form-item label="单位介绍:">{{ orgInfoRecord.description }}</el-form-item>
      </el-form>
    </model-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      inviteUrl: '',
      dialogVisible: false,
      orgInfoRecord: {}
    }
  },
  created() {
    this.inviteUrl = window.location.protocol + '//' + window.location.host + '/#/invite/'
  },
  methods: {
    handleJoinOrg() {
      this.$prompt('请输入组织邀请码:', '组织加入', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPlaceholder: '请输入邀请码',
        inputPattern: /^[0-9a-zA-Z].{5,20}$/,
        inputErrorMessage: '请输入组织邀请码'
      }).then(({ value }) => {
        this.$store.dispatch('checkJoinOrg', value).then((res) => {
          window.location.href = this.inviteUrl + value
        }).catch((_) => {
          this.handleJoinOrg()
        })
      }).catch(() => {
      })
    },
    handleDissolution(item) {
      this.$confirm(`您确定要解散企业【${item.name}】,解散后，该企业的后台管理账号和通讯录将会删除,该操作不可撤销，请谨慎操作， 是否继续?`, '系统提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
        this.$store.dispatch('dissolutionOrg', item.id).then((res) => {
          this.$modal.msgSuccess('企业解散成功！')
          setTimeout(() => {
            this.$store.dispatch('resetToken', res)
            window.location.reload()
          }, 1000)
        })
      }).catch(() => {
      })
    },
    handleLookOrg(item) {
      this.dialogVisible = true
      this.$store.dispatch('upmsOrganizationGetDetailsById', item.id).then((res) => {
        this.orgInfoRecord = res || {}
      })
    },
    leaveOrg(item) {
      this.$confirm(`您确定要退出企业【${item.name}】, 是否继续?`, '退出企业', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('leaveOrg', item.id).then(response => {
          this.$modal.msgSuccess('退出企业成功!')
          this.$store.dispatch('resetToken', response)
          window.location.reload()
        }).catch(() => { })
      }).catch(() => { })
    },
    enterOrg(item) {
      this.$confirm(`您确定切换企业并进入【${item.name}】, 是否继续?`, '进入企业', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('enterOrg', item.id).then(response => {
          this.$modal.msgSuccess('企业切换成功!')
          this.$store.dispatch('resetToken', response)
          setTimeout(() => {
            this.$store.dispatch('getInfo')
          }, 1000)
          this.$emit('success')
        }).catch(() => { })
      }).catch(() => { })
    }

  }
}
</script>
<style lang="scss" scoped>
  .company-container{
    .company-title{
      font-size: 16px;
      font-weight: bold;
      .el-tag{
        margin-left: 8px;
      }
    }
    .box-card{
      margin-bottom: 20px;
      overflow: inherit !important;
    }
    .content{
      display: flex;
      flex-direction: row;
      font-size: 14px;
      cursor: pointer;
      .left,.right{
        width: 80px;
      }
      .right{
        text-align: right;
      }
      .center{
        flex: 1;
        text-align: center;
      }
    }
  }
</style>
